<%@page import="java.sql.*"%>
<%@page import="javax.naming.Context"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.InitialContext"%>
<%
	InitialContext initialContext = new InitialContext();
	Context context = (Context) initialContext.lookup("java:comp/env");
	//The JDBC Data source that we just created
	DataSource ds = (DataSource) context.lookup("jdbc/Meteorologia");
	Connection con = ds.getConnection();

	if (con == null) {
		throw new SQLException("Error establishing connection!");
	}
	
    String userid = request.getParameter("usuario");    
    String pwd = request.getParameter("contrasena");

	Statement st = con.createStatement();
	ResultSet rs;
	rs = st.executeQuery("select * from usuarios where usuario='" + userid
			+ "' and contrasena='" + pwd + "'");
	if (rs.next()) {
		session.setAttribute("userid", userid);
		//out.println("welcome " + userid);
		//out.println("<a href='logout.jsp'>Log out</a>");
		response.sendRedirect("tiempo.jsp");
	} else {
		out.println("Invalid password <a href='index.jsp'>try again</a>");
	}
%>
